require('type')
local class = require('class')
local TextureOperator = require('texture')

-- 精灵类
local Sprite = class()

function Sprite:init(texture)
    self._sprite = nullptr
    if isString(texture) then
        texture = TextureOperator:file(texture)
    end
    if not isPointer(texture) or texture == nullptr then return end
    self._sprite = sprite_create(texture)
    self._x = 0
    self._y = 0
    self._scale = 1
    self:setTextureRect()
end


-- 设置纹理
function Sprite:setTexture(texture)
    if self._sprite == nullptr or not isPointer(texture) or texture == nullptr then return end
    return sprite_setTexture(self._sprite, texture)
end

-- 设置纹理区域
function Sprite:setTextureRect(x, y, w, h)
    if self._sprite == nullptr then return end
    local texture = self:getTexture()
    if texture == nullptr then return end
    if not isNumber(x) then x = 0 end
    if not isNumber(y) then y = 0 end
    TextureOperator:set(texture)
    if not isNumber(w) then w = TextureOperator:getWidth() end
    if not isNumber(h) then h = TextureOperator:getHeight() end
    return sprite_setTextureRect(self._sprite, x, y, w, h)
end


-- 取纹理
function Sprite:getTexture()
	if self._sprite == nullptr then return nullptr end
	return sprite_getTexture(self._sprite)
end

-- 颜色
function Sprite:setColor(color)
	if self._sprite == nullptr then return end
	return sprite_setColor(self._font, isNumber(color) and color or 0xFFFFFFFF)
end

function Sprite:getColor()
	if self._sprite == nullptr then return 0xFFFFFFFF end
	return sprite_getColor(self._sprite)
end


function Sprite:setPositionEx()
    -- return sprite_setPosition(self._sprite, self._x, self._y)
    return sprite_setPositionEx(self._sprite, self._x, self._y, self._scale)
end


function Sprite:setPosition(x, y)
    if self._sprite == nullptr then return end
    self._x = isNumber(x) and x or 0
    self._y = isNumber(y) and y or 0
    return self:setPositionEx()
end

function Sprite:setPositionX(x)
    if self._sprite == nullptr then return end
    self._x = isNumber(x) and x or 0
    return self:setPositionEx()
end

function Sprite:setPositionY(y)
    if self._sprite == nullptr then return end
    self._y = isNumber(y) and y or 0
    return self:setPositionEx()
end

function Sprite:getPosition()
    return self._x, self._y
end

function Sprite:getPositionX()
    return self._x
end

function Sprite:getPositionY()
    return self._y
end

function Sprite:setScale(scale)
    if self._sprite == nullptr then return end
    self._scale = isNumber(scale) and scale or 1
	return self:setPositionEx()
end

function Sprite:getScale()
    return self._scale
end

-- 宽高
function Sprite:getSize()
	if self._sprite == nullptr then return 0, 0 end
	return sprite_getWidth(self._sprite), sprite_getHeight(self._sprite)
end

function Sprite:getWidth()
	if self._sprite == nullptr then return 0 end
	return sprite_getWidth(self._sprite)
end

function Sprite:getHeight()
	if self._sprite == nullptr then return 0 end
	return sprite_getHeight(self._sprite)
end

-- 锚点
function Sprite:setHotSpot(x, y)
	if self._sprite == nullptr then return end
    sprite_setHotSpot(self._sprite, isNumber(x) and x or 0, isNumber(y) and y or 0)
    return self:setPositionEx()
end

function Sprite:setMiddle()
	if self._sprite == nullptr then return end
    sprite_setHotSpot(self._sprite, sprite_getWidth(self._sprite) / 2, sprite_getHeight(self._sprite) / 2)
    return self:setPositionEx()
end

-- 渲染
function Sprite:render(x, y)
	if self._sprite == nullptr then return end
    sprite_render(self._sprite, isNumber(x) and x or 0, isNumber(y) and y or 0)
end

function Sprite:renderPosition()
	if self._sprite == nullptr then return end
    sprite_renderPosition(self._sprite)
end

-- 销毁
function Sprite:release()
    if self._sprite == nullptr then return end
    sprite_release(self._sprite)
    self._sprite = nullptr
end



function Sprite:汉化()
	self.设纹理 = self.setTexture
    self.取纹理 = self.getTexture
    self.设纹理区域 = self.setTextureRect
	self.设颜色 = self.setColor
	self.取颜色 = self.getColor
    self.设坐标 = self.setPosition
    self.设坐标x = self.setPositionX
    self.设坐标y = self.setPositionY
    self.取坐标 = self.getPosition
    self.取坐标x = self.getPositionX
    self.取坐标y = self.getPositionY
    self.设缩放 = self.setScale
    self.取缩放 = self.getScale
	self.取宽高 = self.getSize
	self.取宽度 = self.getWidth
    self.取高度 = self.getHeight
    self.设锚点 = self.setHotSpot
    self.设锚点居中 = self.setMiddle
    -- self.渲染 = self.render
    self.渲染 = self.renderPosition
	self.销毁 = self.release
end



return Sprite